const pingNianDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const runNianDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

class DateTimeHelper {

  static addTime(start, factor) {
    let copy = {
      month: 0,
      day: 0,
      hour: 0,
      minute: 0,
      getTime: function () {//TODO 这里有一个很奇怪的bug，如果按照常理，下面这个应该写为 this.minute<10，可以必须要写成<9,什么鬼？
        return (this.hour < 10 ? ('0' + this.hour) : this.hour) + ":" + ((this.minute.toFixed(0) + '').length > 1 ? this.minute.toFixed(0) : ('0' + this.minute.toFixed(0)));
      },
      getDate: function () {
        return this.month + "/" + this.day;
      }
    };
    let minutes = factor % 60;    // 取剩余分钟数
    let hours = (factor - minutes) / 60;  //取小时数
    copy.minute = start.minute + minutes;
    if (copy.minute >= 60) {
      copy.hour += 1;
      copy.minute -= 60;
    }

    copy.hour += hours;
    copy.hour += start.hour;
    let hourCount = copy.hour;    //记录总hour数
    if (copy.hour >= 24) {
      copy.day += Math.floor(copy.hour/24);
      copy.hour -= 24 * Math.floor(copy.hour/24);
    }

    copy.day += start.day;

    copy.month = start.month;
    copy.year = start.year;
    //做好判断，针对月份的
    if (DateTimeHelper.isLeapYear(start.year || new Date().getUTCFullYear())) {
      if (copy.day > runNianDays[start.month - 1]) {
        copy.day = Math.floor(hourCount/24);
        copy.month += 1;
      }
    } else {
      if (copy.day > pingNianDays[start.month - 1]) {
        copy.day = Math.floor(hourCount/24);
        copy.month += 1;
      }
    }

    if (copy.month > 12) {
      copy.month = 1;
      copy.year += 1;
    }

    return copy;
  }

  static isLeapYear(year) {
    return (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0);
  }

  /**
   * 获取多少个小时
   * @param startTime
   * @param endTime
   * @returns {number}
   */
  static getDuration(startTime, endTime) {

    var date1 = `${endTime.year ? endTime.year : new Date().getUTCFullYear()}/${endTime.month}/${endTime.day} ${endTime.hour}:00:00`;  //开始时间
    var date2 = `${startTime.year ? startTime.year : new Date().getUTCFullYear()}/${startTime.month}/${startTime.day} ${startTime.hour}:00:00`;    //结束时间
    var date3 = new Date(date1).getTime() - new Date(date2).getTime();   //时间差的毫秒数

    return date3 / 1000.0 / 60.0;
  }

  static getTimestamp(date) {
    //"2014-07-10 10:21:12"
    let d = new Date(`${date.year}-${date.month}-${date.day} ${date.hour}:${date.minute}:00`);
    return d.getTime();
  }

  static timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D + h + m + s;
  }


  static getMonthDay(month) {
    let day31 = [1, 3, 5, 7, 8, 10, 12]
    let day30 = [4, 6, 9, 11];
    if (day31.indexOf(month) != -1) {
      return 31;
    } else if (day30.indexOf(month) != -1) {
      return 30;
    } else {
      if (new Date().getFullYear() % 4 == 0 || new Date().getFullYear() % 100 == 0) {
        return 29;
      } else {
        return 28;
      }
    }
  }


  static formCurDate() {         //今天的日期
    let date = new Date();
    return date.getFullYear() + "." + this.formTime(date.getMonth() + 1) + "." + this.formTime(date.getDate()) + " " + this.formTime(date.getHours()) + ":00";
  }

  static getCurDateNow() {   //当前日期   格式为  20180706071700
    let date = new Date();
    return String(''+ date.getFullYear() + this.formTime(date.getMonth() + 1) + this.formTime(date.getDate()) + this.formTime(date.getHours()) + this.formTime(date.getMinutes()) + this.formTime(date.getSeconds()));
  }

  static formPreDate() {            //前一天的日期
    let cur_year = new Date().getFullYear();
    let cur_month = new Date().getMonth() + 1;
    let cur_day = new Date().getDate();
    if (cur_day - 1 > 0) {
      let day = cur_day - 1;
      return cur_year + "." + this.formTime(cur_month) + "." + this.formTime(day) + " " + this.formTime(new Date().getHours()) + ":00";
    } else {
      let month = cur_month - 1 > 0 ? cur_month - 1 : 12;
      return this.formPreMonth(cur_month) + "." + this.getMonthDay(month) + " " + this.formTime(new Date().getHours()) + ":00";
    }
  }
  static formPreDefineDate(count) {            //前九个小时日期
    let cur_year = new Date().getFullYear();
    let cur_month = new Date().getMonth() + 1;
    let cur_day = new Date().getDate();
    let cur_hour = new Date().getHours();   //获取当前小时
    if(cur_hour-count > 0){
      return cur_year + "." + this.formTime(cur_month) + "." + this.formTime(cur_day) + " " + this.formTime(cur_hour-count) + ":00";
    }else if(cur_day > 1){    //减一天
      return cur_year + "." + this.formTime(cur_month) + "." + this.formTime(cur_day-1) + " " + this.formTime(cur_hour-count+24) + ":00";
    }else if(cur_day === 1){    //减一个月份、年份
      return cur_month > 1 ? cur_year + "." + this.formTime(cur_month-1) + "." + this.getMonthDay(cur_month-1)+" " + this.formTime(cur_hour-count+24) + ":00"
        :(cur_year-1) + ".12.31"+ " " + this.formTime(cur_hour-count+24) + ":00";
    }
  }

  static formPreMonth(month) {     //1-12
    let preMonth = month - 1;
    if (preMonth > 0) {
      return new Date().getFullYear() + "." + this.formTime(preMonth);
    } else {
      return (new Date().getFullYear() - 1) + "." + 12;
    }
  }

  static formTime(time) {    //01格式
    if (time < 10) {
      return "0" + time;
    } else {
      return time;
    }
  }

  static formatTimeLine(time){
    if (time < 60) {
      return time + "秒"
    } else if (time < 3600) {
      let minute = time / 60;
      let second = time % 60;
      return second === 0 ? minute + "分钟" : minute + "分" + second + "秒";
    } else if (time < 216000) {
      let hour = time / 3600;
      let minute = (time % 3600) / 60;
      let second = (time % 3600) % 60;
      return minute === 0 ? hour + "小时" : hour + "时" + minute + "分" + second + "秒";
    } else {

    }

  }

  // 得到下一天（20181231->20190101）
  static getNextDay(dateToday) {
    dateToday = String(dateToday);
    const date = new Date(dateToday.slice(0, 4), parseInt(dateToday.slice(4, 6)) - 1, parseInt(dateToday.slice(6, 8)));
    date.setDate(date.getDate() + 1);
    return String(date.getFullYear()) + this.formTime(date.getMonth() + 1) + this.formTime(date.getDate());
  }

  // 返回输入时间变化相应秒数得到的结果
  // 20180708152600, (-300)s => 20180708152100
  static getNewTimeBySecends(dateToday, seconds) {
    dateToday = String(dateToday);
    seconds = parseInt(seconds);
    const date = new Date(dateToday.slice(0, 4), parseInt(dateToday.slice(4, 6)) - 1, parseInt(dateToday.slice(6, 8)), parseInt(dateToday.slice(8, 10)), parseInt(dateToday.slice(10, 12)), parseInt(dateToday.slice(12, 14)));
    date.setSeconds(date.getSeconds() + seconds);
    return String(date.getFullYear()) + this.formTime(date.getMonth() + 1) + this.formTime(date.getDate()) + this.formTime(date.getHours()) + this.formTime(date.getMinutes()) + this.formTime(date.getSeconds());
  }

  // 当前时间前n分钟（20180708101000->20180708104500）参数-间隔分钟数
  static getMinuteTime(len) {
    len = String(len);
    let curTime = new Date();
    curTime.setMinutes(curTime.getMinutes() - len);
    return String(curTime.getFullYear()) + this.formTime(curTime.getMonth() + 1) + this.formTime(curTime.getDate()) + this.formTime(curTime.getHours())+ this.formTime(curTime.getMinutes()) + this.formTime(curTime.getSeconds());
  }
  // 当前时间后n分钟（20180708101000->20180708104500）参数-间隔分钟数
  static getAfterMinuteTime(len){
    //len = String(len);
    let curTime = new Date();
    curTime.setMinutes(curTime.getMinutes() + len);
    return String(curTime.getFullYear()) + this.formTime(curTime.getMonth() + 1) + this.formTime(curTime.getDate()) + this.formTime(curTime.getHours())+ this.formTime(curTime.getMinutes()) + this.formTime(curTime.getSeconds());
  }

  //当前时间：20180708121010
  static getCurTime(){
    let curTime = new Date();
    return String(curTime.getFullYear()) + this.formTime(curTime.getMonth() + 1) + this.formTime(curTime.getDate()) + this.formTime(curTime.getHours())+ this.formTime(curTime.getMinutes()) + this.formTime(curTime.getSeconds());
  }

  //当前日期：20180708
  static getCurDate(){
    let curTime = new Date();
    return String(curTime.getFullYear()) + this.formTime(curTime.getMonth() + 1) + this.formTime(curTime.getDate());
  }

  //20180718071223  转成  2018-07-18 07:12:23
  static returnStandardDate(dateStr){
    var yearNum = dateStr.substr(0,4);
    var monthNum = dateStr.substr(4,2);
    var dayNum = dateStr.substr(6,2);
    var hourNum = dateStr.substr(8,2);
    var minuteNum = dateStr.substr(10,2);
    var secondNum = dateStr.substr(12,2);
    //return newDate;
    return String(yearNum+'-'+monthNum+'-'+dayNum+' '+hourNum+':'+minuteNum+':'+secondNum);
  }

  static get3MonthBefor(){  //获取3个月前的日期
    var year,month,date;
    var currDate = new Date();
    var resultDate = {};
    year = currDate.getFullYear();
    month = currDate.getMonth()+1;
    date = currDate.getDate();
    switch(month)
    {
      case 1:
      case 2:
      case 3:
        month += 9;
        year--;
        break;
      default:
        month -= 3;
        break;
    }
    resultDate.year = year;
    resultDate.month = month;
    resultDate.date = date;
   // resultDate = year + '-'+month+'-'+date+' ' + hms;
    return resultDate;
  }

  static getAfterDayNew(afterDate){
    let flag = false;
    var currDate = new Date();
    currDate = new Date(currDate.getFullYear() + "-" + (currDate.getMonth() + 1) + "-" + (currDate.getDate()-1));
    afterDate = new Date(afterDate);
    var n = currDate.getTime() - afterDate.getTime();
    if(n<0){
      flag = true;
    }
    return flag;
  }
}

export default DateTimeHelper;
